package com.bigegg.employee.service.impl;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

import com.alibaba.excel.EasyExcel;
import com.bigegg.employee.domain.Classes;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.domain.Major;
import com.bigegg.employee.mapper.ClassesMapper;
import com.bigegg.employee.mapper.CollegeMapper;
import com.bigegg.employee.mapper.MajorMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bigegg.employee.mapper.SourceStudentMapper;
import com.bigegg.employee.domain.SourceStudent;
import com.bigegg.employee.service.ISourceStudentService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 生源信息Service业务层处理
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@Service
public class SourceStudentServiceImpl implements ISourceStudentService 
{

    public final Logger log = LoggerFactory.getLogger(SourceStudentServiceImpl.class);

    @Autowired
    private SourceStudentMapper sourceStudentMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private CollegeMapper collegeMapper;

    @Autowired
    private ClassesMapper classesMapper;

    /**
     * 查询生源信息
     * 
     * @param id 生源信息主键
     * @return 生源信息
     */
    @Override
    public SourceStudent selectSourceStudentById(Long id)
    {
        return sourceStudentMapper.selectSourceStudentById(id);
    }

    /**
     * 查询生源信息列表
     * 
     * @param sourceStudent 生源信息
     * @return 生源信息
     */
    @Override
    public List<SourceStudent> selectSourceStudentList(SourceStudent sourceStudent)
    {
        return sourceStudentMapper.selectSourceStudentList(sourceStudent);
    }

    /**
     * 新增生源信息
     * 
     * @param sourceStudent 生源信息
     * @return 结果
     */
    @Override
    public int insertSourceStudent(SourceStudent sourceStudent)
    {
        return sourceStudentMapper.insertSourceStudent(sourceStudent);
    }

    /**
     * 修改生源信息
     * 
     * @param sourceStudent 生源信息
     * @return 结果
     */
    @Override
    public int updateSourceStudent(SourceStudent sourceStudent)
    {
        return sourceStudentMapper.updateSourceStudent(sourceStudent);
    }

    /**
     * 批量删除生源信息
     * 
     * @param ids 需要删除的生源信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceStudentByIds(Long[] ids)
    {
        return sourceStudentMapper.deleteSourceStudentByIds(ids);
    }

    /**
     * 删除生源信息信息
     * 
     * @param id 生源信息主键
     * @return 结果
     */
    @Override
    public int deleteSourceStudentById(Long id)
    {
        return sourceStudentMapper.deleteSourceStudentById(id);
    }

    /**
     * 上传excel
     */
    @Override
    public void importData(MultipartFile multipartFileile){

        InputStream is = null;
        try {
            is = multipartFileile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<SourceStudent> list = EasyExcel.read(new BufferedInputStream(is)).head(SourceStudent.class).sheet().headRowNumber(1).doReadSync();

        if(list!=null && list.size()>0){

            Long cnt = 0L;

            for(SourceStudent sourceStudent : list){

                //
                SourceStudent c = new SourceStudent();

                c.setStuNo(sourceStudent.getStuNo());

                List<SourceStudent> sourceStudentList = this.sourceStudentMapper.selectSourceStudentList(c);

                if(sourceStudentList==null || sourceStudentList.size()==0){

                    sourceStudent.setCreateTime(new Date());

                    // 学院id
                    College college = new College();
                    college.setName(sourceStudent.getCollegeName());
                    List<College> collegeList = this.collegeMapper.selectCollegeList(college);
                    if(collegeList!=null && collegeList.size()>0){
                        College col = collegeList.get(0);
                        sourceStudent.setCollegeId(col.getId());
                    }

                    // 专业
                    Major major = new Major();
                    major.setName(sourceStudent.getMajorName());
                    List<Major> majorList = this.majorMapper.selectMajorList(major);
                    if(majorList!=null && majorList.size()>0){
                        Major col = majorList.get(0);
                        sourceStudent.setMajorId(col.getId());
                    }

                    // 班级
                    Classes classes = new Classes();
                    classes.setName(sourceStudent.getClassesName());
                    List<Classes> classesList = this.classesMapper.selectClassesList(classes);
                    if(classesList!=null && classesList.size()>0){
                        Classes cl = classesList.get(0);
                        sourceStudent.setClassesId(cl.getId());
                    }

                    this.sourceStudentMapper.insertSourceStudent(sourceStudent);

                }else {

                    this.log.info("此条已存在，无需新增，进行关联");
                }

                cnt++;
                this.log.info(String.format("已完成%d/%d",cnt,list.size()));
            }

        }
    }
}
